/* 
 * Shuriken is the legal property of its developers. Please refer to the
 * COPYRIGHT file distributed with this
 * source distribution.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.Collections.Generic;
using System.Text;

namespace Shuriken.Core.Ranking
{
    /// <summary>
    /// Representing a range 
    /// </summary>
    /// <remarks>
    /// from http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Java/Classes/NSRange.html#//apple_ref/java/instm/com.apple.cocoa.foundation.NSRange/NSRange
    /// An NSRange represents a range, a measurement of a segment of something linear, such as a byte stream. An NSRange has two primary values, a location and a length. The methods of NSRange give access to these values; convert between NSRanges and their string representations; test and compare ranges; and create ranges based on operations involving the union, intersection, and subtraction of two ranges.
    /// </remarks>
    public class Range
    {
        public static int NotFound = 0x7fffffff;
        public static Range NullRange = new Range(Range.NotFound, 0);

        int _length;
        int _location;

        public Range(int location, int length)
        {
            this._length = length;
            this._location = location;
        }

        public Range(Range range) : this(range.Location, range.Length) { }

        public int Length
        {
            get
            {
                return _length;
            }

            set
            {
                _length = value;
            }
        }

        public int Location
        {
            get
            {
                return _location;
            }

            set
            {
                _location = value;
            }
        }

        /// <summary>
        /// Returns whether the location aLocation comes after or matches the starting location and comes before the ending location of the receiver.
        /// </summary>
        /// <param name="location"></param>
        /// <returns></returns>
        public bool LocationInRange(int location)
        {
            if (location < _location)
            {
                return false;
            }

            if (_location + _length >= location)
            {
                return false;
            }

            return true;
        }


        /// <summary>
        /// Returns the extent of the receiver (its starting location plus its length).
        /// </summary>
        /// <remarks>
        /// This number is 1 greater than the last location in the range.
        /// </remarks>
        public int MaxRange
        {
            get
            {
                return _location + _length;
            }
        }

        /// <summary>
        /// Returns whether the length of the receiver is 0.
        /// </summary>
        public bool IsEmpty
        {
            get
            {
                if (_length == 0)
                {
                    return true;
                }

                return false;
            }
        }

        /// <summary>
        /// Creates an LPRange out of parameters
        /// </summary>
        /// <param name="loc">location</param>
        /// <param name="lenght">length</param>
        /// <returns>range</returns>
        public static Range MakeRange(int loc, int lenght)
        {
            return new Range(loc, lenght);
        }

        /// <summary>
        ///  returns the next location after the range passed to it. 
        /// </summary>
        /// <param name="range">range</param>
        /// <returns>next to this range</returns>
        public static int GetMaxRange(Range range)
        {
            return range.Location + range.Length;
        }


        /// <summary>
        /// Returns the string representation of the Range.
        /// </summary>
        /// <returns>string representation of this structure</returns>
        public override string ToString()
        {
            return "Range(Location=" + Location + ",Length=" + Length + ")";
        }
    }
}
